<?php
       
       
       $investigador= utf8_decode($_GET['investigador']);
       $ininv=$_GET['ininv'];
       $finv=$_GET['finv'];
       $quotes=$_GET['quotes'];
       /*$investigador=  utf8_decode("Alfredo Torres Larios");
       $ininv=1962;
       $finv=2014;
       $quotes='isi';
    */
       
       $fd=  fopen("archivos/datosinv.txt", "w");
        //Se almacena la conexion en la variable $con
        
        $con=mysqli_connect("192.168.1.10","srojas","anemona") or die("Failed to connect with database!!!!");
        mysqli_select_db( $con,"hermes2"); 
        
        
        if($investigador==='Todos'){
            $sth=  mysqli_query($con, "select p.nombre as inv, st.hindex as hindx from ifcweb_investigadores.perfiles as p left outer join hermes2.stat_inv as st on p.id=st.id_inv order by hindx asc; ");
            
            
            
            
            $rows = array();
            $table = array();
            $table['cols'] = array(

            // Labels for your chart, these represent the column titles
            // Note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for column title
            array('label' => 'Investigador', 'type' => 'string'),
            array('label' => 'HIndex', 'type' => 'number')

            );
            
            fwrite($fd, "Investigador   hindex"."\r\n");
            while($r = mysqli_fetch_assoc($sth)) {
                fwrite($fd,"\t".$r['inv']."\t");
                fwrite($fd, $r['hindx']."\r\n");
            $temp = array();
            // the following line will be used to slice the Pie chart
            $temp[] = array('v' => (string) utf8_encode($r['inv'])); 

            // Values of each slice
           
            $temp[] = array('v' => (int) $r['hindx']);
            
            
            $rows[] = array('c' => $temp);
            }

            $table['rows'] = $rows;



            fclose($fd);
            
            
        }else{
        
        
       if($quotes==='isi'){
            $sth = mysqli_query($con,"select publicacion_year, count(*) as publicaciones, avg(citas_isi) as isi from hermes2.papers_vs_citas as pc "
                    . "join ifcweb_investigadores.perfiles as p on pc.autor_id=p.id where publicacion_year>=$ininv and publicacion_year<=$finv and p.nombre='$investigador'"
                    . "and pc.doi is not null group by publicacion_year;");





            $rows = array();
            $table = array();
            $table['cols'] = array(

            // Labels for your chart, these represent the column titles
            // Note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for column title
            array('label' => 'publicacion_year', 'type' => 'string'),
            array('label' => 'Publicaciones', 'type' => 'number'),
            array('label'=> 'isi','type'=>'number')

            );
            fwrite($fd,$investigador."\r\n");
            fwrite($fd, "publicacion_year   num_publicaciones   citas_isi"."\r\n");
            while($r = mysqli_fetch_assoc($sth)) {
                fwrite($fd,"\t".$r['publicacion_year']."\t");
                fwrite($fd, $r['publicaciones']."\t");
                fwrite($fd, $r['isi']."\r\n");
            $temp = array();
            // the following line will be used to slice the Pie chart
            $temp[] = array('v' => (string) utf8_encode($r['publicacion_year'])); 

            // Values of each slice
           
            $temp[] = array('v' => (int) $r['publicaciones']);
            
            if($r['isi']==='1.0000'){
                $temp[] = array('v' => (float) 1.1);
            }else{ 
                $temp[] = array('v' => (float) $r['isi']);
            }
            $rows[] = array('c' => $temp);
            }

            $table['rows'] = $rows;



            fclose($fd);
       }elseif ($quotes==='scopus') {
           $sth = mysqli_query($con,"select publicacion_year, count(*) as publicaciones, avg(citas_scopus) as scopus from hermes2.papers_vs_citas as pc "
                   . "join ifcweb_investigadores.perfiles as p on pc.autor_id=p.id where publicacion_year>=$ininv and publicacion_year<=$finv and p.nombre='$investigador' "
                   . "and pc.doi is not null group by publicacion_year;");
        
       
 
        
        $rows = array();
        $table = array();
        $table['cols'] = array(

        // Labels for your chart, these represent the column titles
        // Note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for column title
        array('label' => 'publicacion_year', 'type' => 'string'),
        array('label' => 'Publicaciones', 'type' => 'number'),
        array('label'=> 'scopus','type'=>'number')
                
        );
        fwrite($fd,$investigador."\r\n");
        fwrite($fd, "publicacion_year   num_publicaciones   citas_scopus"."\r\n");
        while($r = mysqli_fetch_assoc($sth)) {
            fwrite($fd,"\t".$r['publicacion_year']."\t");
            fwrite($fd, $r['publicaciones']."\t");
            fwrite($fd, $r['scopus']."\r\n");
        $temp = array();
        // the following line will be used to slice the Pie chart
        $temp[] = array('v' => (string) utf8_encode($r['publicacion_year'])); 

        // Values of each slice
        
       
        $temp[] = array('v' => (int) $r['publicaciones']); 
        
        if($r['scopus']==='1.0000'){
            $temp[] = array('v' => (float) 1.1);
        }else{
            $temp[] = array('v' => (float) $r['scopus']);
        }
        
        $rows[] = array('c' => $temp);
        }

        $table['rows'] = $rows;
        
       
      
        fclose($fd);
    
}elseif ($quotes==='isiscopus') {
    $sth = mysqli_query($con,"select publicacion_year, count(*) as publicaciones, avg(citas_isi) as isi, avg(citas_scopus) as scopus from hermes2.papers_vs_citas as pc "
            . "join ifcweb_investigadores.perfiles as p on pc.autor_id=p.id where publicacion_year>=$ininv and publicacion_year<=$finv and p.nombre='$investigador' "
            . "and pc.doi is not null group by publicacion_year;");
        
   
        
        $rows = array();
        $table = array();
        $table['cols'] = array(

        // Labels for your chart, these represent the column titles
        // Note that one column is in "string" format and another one is in "number" format as pie chart only required "numbers" for calculating percentage and string will be used for column title
        array('label' => 'publicacion_year', 'type' => 'string'),
        array('label' => 'Publicaciones', 'type' => 'number'),
        array('label'=> 'isi','type'=>'number'),
        array('label'=> 'scopus','type'=>'number')
                
        );
        fwrite($fd,$investigador."\r\n");
        fwrite($fd, "publicacion_year   num_publicaciones   citas_isi   citas_scopus"."\r\n");
        while($r = mysqli_fetch_assoc($sth)) {
            fwrite($fd,$r['publicacion_year']."\t");
            fwrite($fd, $r['publicaciones']."\t");
            fwrite($fd, $r['isi']."\t");
            fwrite($fd, $r['scopus']."\r\n");
        $temp = array();
        // the following line will be used to slice the Pie chart
        $temp[] = array('v' => (string) utf8_encode($r['publicacion_year'])); 

        // Values of each slice
        
        $temp[] = array('v' => (int) $r['publicaciones']);
        
        if($r['isi']==='1.0000'){
            $temp[] = array('v' => (float)1.1);
        }else{
            $temp[] = array('v' => (float) $r['isi']);
        } 
        if($r['scopus']==='1.0000'){
            $temp[] = array('v' => (float) 1.1);
        }else{
            $temp[] = array('v' => (float) $r['scopus']);
        }
        
        
        $rows[] = array('c' => $temp);
        }

        $table['rows'] = $rows;
        
       
      
        fclose($fd);
    
}
        }
  mysqli_close($con);
        
        echo json_encode($table)
        
        ?>
